﻿using Dx.DDD.Domain;
using Dx.DDD.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Dx.DDD.Repositories
{
    public class Repository<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity : class,IEntity<TKey>
    {
        public  DbContext DbContext { get; set; }
        public Repository(
            DbContext dbContext
            ) 
        {
            DbContext=dbContext;
        }

        public async Task<TEntity> AddAsync(TEntity entity, bool autoSave = true, CancellationToken cancellationToken = default)
        {
            await DbContext.Set<TEntity>().AddAsync(entity);
            if (autoSave)
            {
                await DbContext.SaveChangesAsync(cancellationToken);
            }

            return entity;
        }

        public async Task<TEntity?> DeleteAsync(TKey? key, bool autoSave = true, CancellationToken cancellationToken = default)
        {
            var dbSet = await DbContext.Set<TEntity>().FindAsync(key);
            if (dbSet != null)
            {
                DbContext.Set<TEntity>().Remove(dbSet);
                if (autoSave)
                {
                    await DbContext.SaveChangesAsync();
                }
            }
            return dbSet;
        }

        public async Task<TEntity?> GetAsync(TKey? key, CancellationToken cancellationToken = default)
        {
            return await DbContext.Set<TEntity>().FindAsync(key);
        }
        public async Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = true, CancellationToken cancellationToken = default(CancellationToken))
        {
            return await DbContext.Set<TEntity>().AsNoTracking().Where(predicate).SingleOrDefaultAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
        }
        public async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = true, CancellationToken cancellationToken = default)
        {
            var updatedEntity = DbContext.Update(entity).Entity;
            if (autoSave)
            {
                await DbContext.SaveChangesAsync(cancellationToken);
            }

            return updatedEntity;
        }

        public async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>>? predicate, CancellationToken cancellationToken = default)
        {
            while (predicate==null)
            {
                return await DbContext.Set<TEntity>().AsNoTracking().ToListAsync(cancellationToken);
            }
           return await DbContext.Set<TEntity>().AsNoTracking().Where(predicate).ToListAsync(cancellationToken);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="condition">是否有条件</param>
        /// <param name="predicate">查询条件</param>
        /// <returns></returns>
        public IQueryable<TEntity> WhereIf( bool condition, Expression<Func<TEntity, bool>> predicate)
        {
            if (!condition)
            {
                return  DbContext.Set<TEntity>();
            }
            return DbContext.Set<TEntity>().AsNoTracking().Where(predicate);
        }
    }
}
